package com.tsfot.spider.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tsfot.spider.exception.FileNotFindException;

public class RollingFileService{
	
	private final String FILE_DEFAULT_ENCODING = "UTF-8";
	//private final int FILE_STORAGE_DAYS = 7;
	
	private Logger logger = LoggerFactory.getLogger(RollingFileService.class);
	
	private String basePath;
	
	private String filePath;
	
	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getBasePath() {
		return basePath;
	}
	
	public void init(){
		
	}
	

	public void setBasePath(String basePath) {
		File file = new File(basePath);
		if(file.exists()&&file.isDirectory()){
			this.basePath = basePath;
		}else{
			logger.error("directory ["+basePath+"] not find.please create a directory["+basePath+"]");
			throw new FileNotFindException("directory ["+basePath+"] not find.");
		}
		//创建基础目录
		createRollingDirecotry();
	}
	
	
	
	private void createRollingDirecotry(){
		try {
			FileUtils.forceMkdir(new File(getSavePath()));
		} catch (IOException e) {
			logger.error("create directory ["+(filePath)+"] failed.");
		}
	}
	
	public String writeStringToFile(String name,String content){
		String savePath=getSavePath()+File.separatorChar+name;
		try {
			FileUtils.writeStringToFile(new File(savePath), content, FILE_DEFAULT_ENCODING);
		} catch (IOException e) {
			
		}
		return savePath;
	}
	
	public String readFileToString(String path){
		
		String content = "";
		try {
			content = FileUtils.readFileToString(new File(path), FILE_DEFAULT_ENCODING);
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}
		return content;
	}
	
	public String getSavePath(){
		return basePath;
	}

	public String writeImageToFile(InputStream in,String fileName) {
		String dirPath=getBasePath()+"/images/";
		String realPath = dirPath.concat(fileName);  
		try { 
            File dir = new File(dirPath);  
            if (dir == null || !dir.exists()) {  
                dir.mkdirs();  
            }  
  
            //文件真实路径  
            File file = new File(realPath);  
            if (file == null || !file.exists()) {  
                file.createNewFile();  
            }  
  
            FileOutputStream fos = new FileOutputStream(file);  
            byte[] buf = new byte[1024];  
            int len = 0;  
            while ((len = in.read(buf)) != -1) {  
                fos.write(buf, 0, len);  
            }  
            fos.flush();  
            fos.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }finally{  
            try {  
                in.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        } 
		return realPath;
	}
	
}
